package com.warehouse_management.purchase.controller;

import com.warehouse_management.config.PageModel;
import com.warehouse_management.config.StringRegex;
import com.warehouse_management.purchase.model.*;
import com.warehouse_management.purchase.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

/**
 * @PACKAGE_NAME: com.warehouse_management.purchase.controller
 * @NAME: PurchaseController
 * @USER: ZhouYongCheng
 * @DATE: 2022/10/3
 * @TIME: 2:50
 * @Description: 采购单管理控制层
 */
@Controller
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    /**
     * 查询采购单
     * 周永成
     * @return
     */
    @RequestMapping("/GetList/{CaiGouDan}/{CaiGouFTime}/{CaiGouLTime}/{FMoneny}/{LMoneny}/{ShengHe}/{totalnum}")
    @ResponseBody
    public PageModel<Purchase> queryDetailsList(
                    @PathVariable("CaiGouDan") String CaiGouDan,
                    @PathVariable("CaiGouFTime") String CaiGouFTime,
                    @PathVariable("CaiGouLTime") String CaiGouLTime,
                    @PathVariable("FMoneny") String FMoneny,
                    @PathVariable("LMoneny") String LMoneny,
                    @PathVariable("ShengHe") String ShengHe,
                    @PathVariable("totalnum") Integer total,
                    PageModel<Purchase> pageModel) {


        pageModel.setPageNum(total);
        PageModel<Purchase> purchasePageModel = purchaseService.queryPurchaseList(
                CaiGouDan,
                CaiGouFTime,
                CaiGouLTime,
                FMoneny,
                LMoneny,
                ShengHe,
                pageModel
        );

        for (Purchase purchase : purchasePageModel.getData()) {

            //审核状态转换
            switch (purchase.getCheckStatus()){
                case "1" : purchase.setCheckStatus("未审核");break;
                case "2" : purchase.setCheckStatus("审核中");break;
                case "3" : purchase.setCheckStatus("审核通过"); break;
                case "4" : purchase.setCheckStatus("审核未通过");break;
            }
            if (purchase.getCreatId() != null){
                //获取id查询并注入(创建者)
                purchase.setCaoZuoName(purchaseService.queryUserByName(purchase.getCreatId()));
            }
            if (purchase.getCheckId() != null){
                //获取id查询并注入(审核者)
                purchase.setShengHeName(purchaseService.queryUserByName(purchase.getCheckId()));
            }
//            if (purchase.getTotalMoney() != null){
//                //转换数字123456 ----->   123,456 并注入
//                purchase.setTotalMoney(StringRegex.subStringToComma(purchase.getTotalMoney(),3));
//            }
        }

        return purchasePageModel;
    }

    /**
     * 获取brand
     * 周永成
     * @return
     */
    @RequestMapping("/brand")
    @ResponseBody
    public List<Brand> getPurchase(){

        //获取品牌集合
        List<Brand> brands = purchaseService.queryBrandList();

        return brands;
    }

    /**
     * 获取类型集合
     * 周永成
     * @param id
     * @return
     */
    @RequestMapping("/type/{id}")
    @ResponseBody
    public List<Type> getTypes(@PathVariable("id") String id){

        List<Type> types = purchaseService.queryTypeList(id);

        return types;
    }
    /**
     * 获取商品集合
     * 周永成
     * @param id
     * @return
     */
    @RequestMapping("/getProduct/{id}")
    @ResponseBody
    public List<Product> getProduct(@PathVariable("id") String id){

        List<Product> products = purchaseService.queryProductList(id);

        return products;
    }

    /**
     * 获取厂商
     * @return
     */
    @RequestMapping("/firm/{productId}")
    @ResponseBody
    public List<Firm> getFirm(@PathVariable("productId") String productId){
        List<Firm> firms = purchaseService.queryFirmList(productId);
        return firms;
    }

    /**
     * 添加采购单
     * 周永成
     * @param purchase
     * @return
     */
    @RequestMapping("/addPurchase")
    @ResponseBody
    public String addPurchase(@RequestBody List<Purchase> purchase){

        List<Details> detailsList = new ArrayList<>();
        List<Purchase> purchaseList = purchase;

        //循环遍历
        for (Purchase purchase1 : purchase) {
            Details details = new Details();
            //获取商品id注入到details
            details.setPurchaseId(purchase1.getPurchaseId());
            //获取数量注入到details
            details.setCount(purchase1.getCount());
            //根据商品id获取商品单价
            String price = purchaseService.getProductPrice(purchase1.getProductId());
            //算总价
            Integer sum = Integer.valueOf(price) * Integer.valueOf(details.getCount());

            details.setTypeId(purchase1.getTypeId());   //注入类型

            details.setBrandId(purchase1.getBrandId()); //注入品牌

            //注入总价
            details.setTotalMoney(String.valueOf(sum));
            purchase1.setTotalMoney(String.valueOf(sum));
            //注入单价
            details.setPurchasePrice(price);

            //注入厂商id
            details.setFirmId(purchase1.getFirmId());
            //获取用户名
            /**
             * 暂时没有用户名
             * String userID = purchaseService.getUserID(purchase1.getCangKuName());
             * purchase1.setCreatId(userID);
             */
            purchase1.setCreatId("6");

            /**-----------------------------------------------
            */

            //注入商品id
            details.setProductId(purchase1.getProductId());
            detailsList.add(details);
        }

        if (purchaseService.FenFaInsert(purchaseList,detailsList) == 1){
            return "1";
        }
        return "-1";
    }

    /**
     * 周永成
     * 查询采购单信息
     * @param purchaseId
     */
    @RequestMapping("/getDetailsList/{purchaseId}")
    @ResponseBody
    public List<Details> updatePurchase(@PathVariable("purchaseId") String purchaseId){

        List<Details> detailsList = purchaseService.queryDetailsByPurchase(purchaseId);

        return detailsList;

    }
}
